#include <stdio.h>
#include <unistd.h>
#include <getopt.h>

int main(int argc, char *argv[])
{
    int ch, i;
        
    /*opterr = 0;

    while ((ch = getopt(argc, argv, ":ab::c:")) != -1)
    {
        printf("optind: %d\n", optind);
        switch (ch)
        {
            case 'a':
                printf("have option: -a\n");
                break;
            case 'b':
                printf("have option: -b: %s\n", optarg);
                break;
            case 'c':
                printf("have option: -c: %s\n", optarg);
                break;
            case '?':
                printf("unknown option: %c\n", (char)optopt);
                break;
            case ':':
                printf("need arg\n");
                break;
        }
    }

    printf("end, optind: %d\n", optind);

    for (i = 0; i < argc; i++) {
        printf("argv[%d] is %s\n", i, argv[i]);
    }*/


    /* for getopt_long */
    struct option longopts[] = {
        {"name", no_argument, NULL, 'n'},
        {"good", no_argument, NULL, 'g'},
        {"love", required_argument, NULL, 'l'},
        {0,0,0,0}
    };

    char *shortopts = "l:ng";

    while ((ch = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1)
    {
        switch(ch)
        {
            case 'n':
                printf("name\n");
                break;
            case 'g':
                printf("good\n");
                break;
            case 'l':
                printf("love is %s\n", optarg);
                break;
        
        }
    }

    return 0;
}
